package cn.kinggm520;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 作者: kinggm Email:731586355@qq.com
 * 时间:  2020-05-13 20:33
 */
@Configuration
public class RabbitMQConfig {

    //    定义正常交换机
    private static final String ORDER_EXCHANGE = "order_exchange";

    //    定义正常队列
    private static final String ORDER_QUEUE = "order_queue";


    //    定义死信交换机
    private static final String ORDER_EXCHANGE_DLX = "order_exchange_dlx";

    //    定义死信队列
    private static final String ORDER_QUEUE_DLX = "order_queue_dlx";


    //  定义正常交换机
    @Bean("order_exchange")
    public Exchange orderExchange() {
        return ExchangeBuilder.topicExchange(ORDER_EXCHANGE).durable(true).build();
    }

    //    声明正常队列
    @Bean("order_queue")
    public Queue order1() {
        return QueueBuilder.durable(ORDER_QUEUE)
                .withArgument("x-dead-letter-exchange", ORDER_EXCHANGE_DLX) //死信交换机名称
                .withArgument("x-dead-letter-routing-key", "dlx.order.cancel") //死信队列的路由key
                .withArgument("x-message-ttl", 10000) // 设置队列的过期时间30秒
                .build();
    }


    //    绑定正常交换机和正常队列
    @Bean
    public Binding bindingExchange1(@Qualifier("order_queue") Queue queue,
                                    @Qualifier("order_exchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("order.#").noargs();
    }


    //  定义死信交换机
    @Bean("order_exchange_dlx")
    public Exchange orderExchangeDLX() {
        return ExchangeBuilder.topicExchange(ORDER_EXCHANGE_DLX).durable(true).build();
    }

    //    声明死信队列
    @Bean("order_queue_dlx")
    public Queue order2() {
        return QueueBuilder.durable(ORDER_QUEUE_DLX).build();
    }


    //    绑定死信交换机和死信队列
    @Bean
    public Binding bindingExchange2(@Qualifier("order_queue_dlx") Queue queue,
                                    @Qualifier("order_exchange_dlx") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx.order.#").noargs();
    }


}
